package org.feng.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

/**
 * 自定义错误处理器
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2022年06月24日 17时10分
 */
@Component
@Slf4j
@Order(-2)
@ControllerAdvice
public class CustomWebExceptionHandler implements WebExceptionHandler {
    /**
     * Handle the given exception. A completion signal through the return value
     * indicates error handling is complete while an error signal indicates the
     * exception is still not handled.
     *
     * @param exchange the current exchange
     * @param ex       the exception to handle
     * @return {@code Mono<Void>} to indicate when exception handling is complete
     */
    @NonNull
    @Override
    public Mono<Void> handle(@NonNull ServerWebExchange exchange, @NonNull Throwable ex) {
        if (ex instanceof RuntimeException) {
            log.error("全局RuntimeException错误处理", ex);
        }
        return Mono.error(ex);
    }
}
